package com.zhuozhengsoft.pageoffice6springboot2back.controller;

import com.zhuozhengsoft.pageoffice.FileSaver;
import com.zhuozhengsoft.pageoffice.OpenModeType;
import com.zhuozhengsoft.pageoffice.PageOfficeCtrl;
import com.zhuozhengsoft.pageoffice.word.DataRegionWriter;
import com.zhuozhengsoft.pageoffice.word.WordDocumentWriter;
import com.zhuozhengsoft.pageoffice6springboot2back.util.GetDirPathUtil;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

@RestController
@RequestMapping(value = "/TaoHong/")
public class TaoHongController {

    //获取doc目录的磁盘路径
    private final String dir = GetDirPathUtil.getDirPath() + "static/doc/";

    public TaoHongController() throws IOException {
    }

    @RequestMapping(value = "index")
    public ModelAndView showindex() {
        ModelAndView mv = new ModelAndView("TaoHong/index");
        return mv;
    }

    @RequestMapping(value = "Word")
    public String showWord(HttpServletRequest request) {
        PageOfficeCtrl poCtrl = new PageOfficeCtrl(request);
        //打开文档
        poCtrl.webOpen("/doc/TaoHong/test.docx", OpenModeType.docNormalEdit, "张三");
        return poCtrl.getHtml();
    }

    @RequestMapping(value = "taoHong")
    public String showtaoHong(HttpServletRequest request) {
        String fileName = "";
        String mbName = request.getParameter("mb");

        PageOfficeCtrl poCtrl = new PageOfficeCtrl(request);
        WordDocumentWriter doc = new WordDocumentWriter();
        poCtrl.setCustomToolbar(false);
        if (mbName != null && mbName.trim() != "") {
            // 选择模板后执行套红
            fileName = mbName;
            // 填充数据和正文内容到“zhengshi.docx”
            DataRegionWriter copies = doc.openDataRegion("PO_Copies");
            copies.setValue("6");
            DataRegionWriter docNum = doc.openDataRegion("PO_DocNum");
            docNum.setValue("001");
            DataRegionWriter issueDate = doc.openDataRegion("PO_IssueDate");
            issueDate.setValue("2013-5-30");
            DataRegionWriter issueDept = doc.openDataRegion("PO_IssueDept");
            issueDept.setValue("开发部");
            DataRegionWriter sTextS = doc.openDataRegion("PO_STextS");
            sTextS.setSubmitAsFile(true);//保存时单独将正文拆分出来保存一份
            sTextS.setValue("[word]/doc/TaoHong/test.docx[/word]");
            DataRegionWriter sTitle = doc.openDataRegion("PO_sTitle");
            sTitle.setValue("北京某公司文件");
            DataRegionWriter topicWords = doc.openDataRegion("PO_TopicWords");
            topicWords.setValue("Pageoffice、 套红");
            poCtrl.setWriter(doc);

        } else {
            //首次加载时，加载正文内容：test.docx
            fileName = "test.docx";
        }
        //打开文档
        poCtrl.webOpen("/doc/TaoHong/" + fileName, OpenModeType.docNormalEdit, "张三");
        return poCtrl.getHtml();
    }

    @RequestMapping(value = "readOnly")
    public String showreadOnly(HttpServletRequest request) {
        PageOfficeCtrl poCtrl = new PageOfficeCtrl(request);
        //打开文档
        poCtrl.webOpen("/doc/TaoHong/zhengshi.docx", OpenModeType.docReadOnly, "张三");
        return poCtrl.getHtml();
    }

    @RequestMapping("save")
    public void save(HttpServletRequest request, HttpServletResponse response) {
        FileSaver fs = new FileSaver(request, response);
        String fileName = request.getParameter("fileName");
        fs.saveToFile(dir + "TaoHong/" + fileName);
        fs.close();
    }

}
